<?php


namespace app\api\controller;

use app\api\service\PlayService;
use app\api\service\OrderService;
use app\api\controller\Luckdraw;
use app\common\controller\Api;
use think\Config;
use app\common\model\OrderProof;
use app\common\model\Collection;
use app\admin\model\Play as PlayModel;
use think\Db;
use think\Cache;
use app\admin\model\Shop;

/**
 * 活动
 * Class Play
 * @package app\api\controller
 */
class Play extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['lists','detail'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected function _initialize() {
        parent::_initialize();
        if (config('domain')=='http://bdbdev.chbcplus.com') {
            // $this->lng = 121.407771;
            // $this->lat = 31.283332;
            $this->lng = 120.118332;
            $this->lat = 30.330562;
        }
    }



    // 考虑新用户专享，只显示同城活动，美团官方
    // 
    public function lists()
    {
        $pageSize = input('pageSize/d', 20);
        $page = input('page/d', 1);
        $type_id = input('type_id',0);// 0-全城试吃1-elm官方2-大牌专享3-返利餐
        $play_label = input('play_label/d', 0);// 1-大牌专享
        $scheme_no = input('scheme_no/d', 0); // 1-无需评价
        $shop_type = input('shop_type/d', 0);
        // $play_type = input('play_type/d', 1);// 1-霸王餐
        $snatchable = input('snatchable/d', 0); // 只看可抢
        $timeslot = input('timeslot/d', 0); // 时段1-早0-9,2-中9-14,3-晚14-24
        $high_rebate = input('high_rebate/d', 0); // 高返利
        $keywords = trim(input('keywords', '')); // 关键词
        $area = input('area','');
        $arr = explode('/', $area);
        $province = isset($arr[0])?$arr[0]:'';
        $city = isset($arr[1])?$arr[1]:'';
        $area = isset($arr[2])?$arr[2]:'';
        $where = ['s.city'=>$city];
        // $where = ['s.city'=>['in',[$city,'杭州市']]];
        $where['g.super_play_no'] = 0;
        if ($play_label==1) $where['g.play_label'] = ['=', $play_label];
        if ($shop_type) {
            $where['g.shop_type'] = ['=', $shop_type];
        } else {
            // 老版本app不显示京东
            if (isset($_SERVER['HTTP_VERSION'])) {
                $version = $_SERVER['HTTP_VERSION'];
                if ($_SERVER['HTTP_PLATFORM']=='App' && $version<160) {
                    $where['g.shop_type'] = ['in', [1,2]];
                }
            }
        }
        if ($snatchable) {
            $where['g.stock'] = ['>', 0];
        }
        if ($scheme_no) {
            $where['g.scheme_no'] = 3;
        }
        if ($keywords) {
            $where['g.shop_name'] = ['like','%'.$keywords.'%'];
        }

        if ($type_id==0) {
            $where['g.play_type'] = ['in', [1,3]];
            if ($high_rebate) {
                $where['g.play_type'] = ['=', 1];
                $where['g.play_label'] = ['=',0];
            }
        } elseif ($type_id==3) { // 返利餐
            $where['g.play_type'] = ['>', 1];
        }
        $order = isset($newOrder) && $newOrder != '' ? $newOrder : 'distance asc,id desc';
        $data = (new PlayService())->getPlayList($where, $this->lat, $this->lng, $order, $page, $high_rebate,$snatchable,$pageSize);
        $this->success('success', $data);
    }


    public function detail()
    {
        $play_no = input('id/d', 0);
        if (!$play_no) $this->error('缺少必要参数');
        $play = (new PlayService())->getPlayDetail($play_no);
        if (!$play) $this->error('活动不存在');
        $play['back_money'] = round($play['back_money'],2);
        $play['user_pay'] = floatval($play['user_pay']);
        if ($play['play_label_text']=='正常活动' || $play['play_label_text']=='新用户专享') {
            $play['play_label_text']='';
        }
        $this->success('活动详情', $play);
    }
}